package com.group.a.client;

import com.group.a.buffer.ReadWriteBuffer;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;

public class NioClient implements Runnable{
    private String ip;
    private int port;

    public NioClient(String ip, int port) {
        this.ip = ip;
        this.port = port;
    }

    public static void main(String[] args) {
        Thread thread = new Thread(new NioClient("127.0.0.1",8888));
        Thread thread2 = new Thread(new NioClient("127.0.0.1",8888));
        thread.start();
        thread2.start();
        try {
            Thread.sleep(600000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.interrupt();
        thread2.interrupt();
    }


    @Override
    public void run() {
        //客户端对象
        try {
            SocketChannel sc = SocketChannel.open();
            sc.configureBlocking(false);
            Selector selector = Selector.open();
            sc.register(selector,SelectionKey.OP_READ | SelectionKey.OP_WRITE, new ReadWriteBuffer());
            sc.connect(new InetSocketAddress(this.ip, this.port));
            /*等待三次握手完成*/
            while (!sc.finishConnect()){
                ;
            }
            while (!Thread.currentThread().isInterrupted()){
                selector.select();
                    Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                    while (iterator.hasNext()){
                        SelectionKey key = iterator.next();
                        iterator.remove();
                        ReadWriteBuffer readWriteBuffer = (ReadWriteBuffer) key.attachment();
                        ByteBuffer readBuffer = readWriteBuffer.getReadBuffer();
                        ByteBuffer writeBuffer = readWriteBuffer.getWriteBuffer();
                        SocketChannel schl = (SocketChannel) key.channel();
                        if (key.isReadable()){
                            System.out.println("轮询到read事件");
                            schl.read(readBuffer);
                            readBuffer.flip();
                            Charset charset = Charset.forName("UTF-8");
                            CharBuffer charBuffer = charset.decode(readBuffer);
                            System.out.println("form server:"+new String(charBuffer.array()));
                            readBuffer.clear();
                        }
                        if (key.isWritable()){
                            System.out.println("轮询到write事件");
                            writeBuffer.put("hello!".getBytes());
                            writeBuffer.flip();
                            schl.write(writeBuffer);
                            writeBuffer.clear();
                        }
                    }
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
